package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    //接收excel表格的上传
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public void upload(@RequestParam("excelFile") MultipartFile excelFile) throws Exception {
        //接收到上传的文件,借助工具类读取excel内容
        List<String[]> list = POIUtils.readExcel(excelFile);

        //将list集合中的每个字符串数组转化为OrderSetting对象
        List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();
        for (String[] str : list) {
            //Date date = new Date(str[0]);
            System.out.println(str[0]);
            Date date = new SimpleDateFormat("yyyy/MM/dd").parse(str[0]);
            System.out.println(date);
            int count = Integer.parseInt(str[1]);
            OrderSetting orderSetting = new OrderSetting(date, count);
            orderSettingList.add(orderSetting);
        }
        // 此时将文件的数据全部解析到了orderSettingList中，
        // 调用service层方法，将数据插入到数据库中
        try {
            orderSettingService.add(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    @RequestMapping("/findOrderSettingByMonth")
    public Result findOrderSettingByMonth(String date){
        //此时获得的参数格式为2020-03

        try {
            //因为前台所需要的数据格式和orderSetting对象不一致
            //需重新定义类，但是值使用一次，就直接用map来封装
            List<Map<String,Object>> list = orderSettingService.findOrderSettingByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    @RequestMapping("/updateNumberByDate")
    //此时从前台传入数据封装了number，date
    public Result updateNumberByDate(@RequestBody OrderSetting orderSetting){

        try {

            orderSettingService.updateNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
